/**
A script for dynamically generating the year, month and date of the birthday fields
for a form. The fields are assumed to be dropdown <select>s.

To use, encode the form as follows:
	<select id="years">
	</select>
	
	<select id="months" onchange="showDates()">
	</select>
	
	<select id="dates">
	</select>

And then,
	<script type="text/javascript" language="javascript" src="[path to this file]"></script>

If you need default values for the date fields, just set the variables defaultMonth,
defaultDate, and defaultYear to the respective defaults.
	
@author The Chad Estioco
@version Second Semester, AY 2010-2011
*/

var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
var maxdays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

//var defaultMonth = 0
//var defaultDate = 0
//var defaultYear = 0

function showMonths(){
	var i = 0
	var limit = months.length
	
	while(i < limit){
		var month_option = document.createElement("option")
		month_option.text = months[i]
		month_option.value = i + 1
		try{
			document.getElementById("months").add(month_option, null)
		} catch(e){
			document.getElementById("months").add(month_option)
		}
		try{
			month_option.selected = i == defaultMonth
		} catch(e){
		}
		i++
	}
}

function showDates(){
	var i = 1
	var febindex = months.indexOf("February")
	var formonth = document.getElementById("months").selectedIndex
	var years = document.getElementById("years")
	var chosenYear = years.options[years.selectedIndex]
	var limit = (formonth == febindex && (parseInt(chosenYear.text) % 4) == 0) ? 29 : maxdays[formonth]
	var dates = document.getElementById("dates")
	
	while(dates.length != 0){
		dates.remove(0)
	}
	
	while(i <= limit){
		var date_option = document.createElement("option")
		date_option.text = i
		date_option.value = i
		try{
			dates.add(date_option, null)
		} catch(e){
			dates.add(date_option)
		}
		try{
			date_option.selected = i == defaultDate
		} catch(e){
		}
		i++
	}
}

function showYears(){
	var i = 2011
	var maxyear = 2011
	
	while(i <= maxyear){
		var year_option = document.createElement("option")
		year_option.text = i
		year_option.value = i
		try{
			document.getElementById("years").add(year_option, null)
		} catch(e){
			document.getElementById("years").add(year_option)
		}
		try{
			year_option.selected = i == defaultYear
		} catch(e){
		}
		i++
	}
}

showYears()
showMonths()
